﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using CoreInmobiliario.Data;

namespace CoreInmobiliario.Business
{
    [DataObject]
    public class Usuario
    {
        public static Usuario GetInstance()
        {
            return new Usuario();
        }

        private static DBDataContext GetDBDataContext()
        {
            Factories.DBDataContextFactory factory = new Factories.DBDataContextFactory();
            return factory.CreateDataContext() as DBDataContext;
        }

        public Data.Usuario GetById(int usuarioId)
        {
            Data.Usuario usuario = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    usuario = dataContext.Usuarios.FirstOrDefault(u => u.UsuarioId.Equals(usuarioId));
                }
                catch (Exception ex)
                {

                }
            }
            return usuario;
        }

        public List<Data.Usuario> GetList()
        {
            List<Data.Usuario> list = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    list = dataContext.Usuarios.ToList();

                }
                catch (Exception ex)
                {

                }
            }
            return list;
        }

        public Data.Usuario GetUsuarioByUsernameAndPassword(string username, string password)
        {
            Data.Usuario usuario = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    usuario = dataContext.Usuarios.FirstOrDefault(u => u.Usuario1.Equals(username) && u.Contrasena.Equals(password));
                }
                catch (Exception ex)
                {

                }
            }
            return usuario;
        }
    }
}
